// wait 和 notify的使用
public class Main {
    // 要求：三个线程，使用notify，分别只能打印A,B,C，要保证是固定按照A、B、C的顺序打印的。
    public static void main(String[] args) throws InterruptedException {
        Object o1 = new Object();
        Object o2 = new Object();
        Thread t1 = new Thread(()->{
            System.out.println("A");
            synchronized (o1){
                o1.notify();
            }
        });
        Thread t2 = new Thread(()->{
            synchronized (o1){
                try {
                    o1.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("B");
            synchronized (o2){
                o2.notify();
            }
        });
        Thread t3 = new Thread(()->{
            synchronized (o2){
                try {
                    o2.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("C");
            }
        });
        t2.start();
        t3.start();
        Thread.sleep(100);
        t1.start();
    }




    public static void main1(String[] args) throws InterruptedException {
        Object object = new Object();
        Thread t1 = new Thread(()->{
            synchronized (object){
                try {
                    System.out.println("t1 wait之前");
                    object.wait();
                    System.out.println("t1 wait之后");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(()->{
//            //为了演示 wait的死等过程
//            try {
//                Thread.sleep(2000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            synchronized (object){
                System.out.println("t2 notify之前");
                object.notify();
                System.out.println("t2 notify之后");

            }
        });
        t1.start();
        Thread.sleep(100);  // 为了让开始调度时先让t1执行，也可以理解为让wait在notify之前执行
        t2.start();
    }
}
